'use strict';

exports.main = async (event, context) => {
	const db = uniCloud.database();
	const addContentCollection = db.collection('add-content');
	
	// 从事件中获取参数
	const { 
		title, 
		category, 
		content, 
		activity_time, 
		location, 
		max_attendees, 
		cover_image = '',
		author_name = '官方',
		author_avatar = '',
		publisher_name,
		status = 'published',
		pinned = false,
		attendee_count = 0
	} = event;
	
	// 参数验证
	if (!title || !title.trim()) {
		return {
			code: 400,
			msg: '活动标题不能为空'
		};
	}
	
	if (!category) {
		return {
			code: 400,
			msg: '活动类型不能为空'
		};
	}
	
	if (!content || !content.trim()) {
		return {
			code: 400,
			msg: '活动描述不能为空'
		};
	}
	
	if (!activity_time) {
		return {
			code: 400,
			msg: '活动时间不能为空'
		};
	}
	
	if (!location || !location.trim()) {
		return {
			code: 400,
			msg: '活动地点不能为空'
		};
	}
	
	if (!max_attendees || max_attendees <= 0) {
		return {
			code: 400,
			msg: '最大参与人数必须大于0'
		};
	}
	
	// 数据类型转换和验证
	const parsedActivityTime = parseInt(activity_time);
	const parsedMaxAttendees = parseInt(max_attendees);
	const parsedAttendeeCount = parseInt(attendee_count);
	
	// 验证时间是否有效
	if (isNaN(parsedActivityTime) || parsedActivityTime <= Date.now()) {
		return {
			code: 400,
			msg: '活动时间必须是未来的有效时间'
		};
	}
	
	// 验证人数是否有效
	if (isNaN(parsedMaxAttendees) || parsedMaxAttendees <= 0) {
		return {
			code: 400,
			msg: '最大参与人数必须是正整数'
		};
	}
	
	if (isNaN(parsedAttendeeCount) || parsedAttendeeCount < 0) {
		return {
			code: 400,
			msg: '当前参与人数不能为负数'
		};
	}
	
	if (parsedAttendeeCount > parsedMaxAttendees) {
		return {
			code: 400,
			msg: '当前参与人数不能超过最大参与人数'
		};
	}
	
	// 构建活动数据
	const currentTime = Date.now();
	const finalAuthorName = author_name || publisher_name || '官方';
	const activityData = {
		content_type: 'activity',
		title: title.trim(),
		category,
		content: content.trim(),
		activity_time: parsedActivityTime,
		location: location.trim(),
		max_attendees: parsedMaxAttendees,
		attendee_count: parsedAttendeeCount,
		cover_image: cover_image || '',
		author_name: finalAuthorName,
		author_avatar: author_avatar || '',
		publisher_name: finalAuthorName,
		user_id: 'official', // 官方活动使用特殊的用户ID
		user_name: finalAuthorName,
		publisher: {
			nickname: finalAuthorName,
			avatar: author_avatar || ''
		},
		user_info: {
			nickname: finalAuthorName,
			username: 'official',
			avatar: author_avatar || ''
		},
		status,
		pinned,
		like_count: 0,
		comment_count: 0,
		violation_count: 0,
		create_time: currentTime,
		last_modified: currentTime,
		files: cover_image ? [cover_image] : []
	};
	
	try {
		// 插入数据到数据库
		const result = await addContentCollection.add(activityData);
		
		console.log('官方活动创建成功:', {
			id: result.id,
			title: activityData.title,
			category: activityData.category,
			activity_time: activityData.activity_time,
			location: activityData.location
		});
		
		return {
			code: 200,
			msg: '官方活动创建成功',
			data: {
				id: result.id,
				...activityData
			}
		};
		
	} catch (error) {
		console.error('创建官方活动失败:', error);
		
		return {
			code: 500,
			msg: '创建官方活动失败',
			error: error.message
		};
	}
};
